<?php
session_start();
Header("Pragma: No-cache");
set_include_path('./../../nearestCaches/library' . PATH_SEPARATOR . get_include_path());
$PEAR_PATH = 'PEAR.php';
include ('Request/Request.php');
include "functions.php";
$lang = getLang();
$versionInt = getClientIntVersion();


if ($_GET['type'] == "offline") {

if((!isset($_SESSION['cookie']))&&(
        (!isset($_POST['login'])&&!isset($_POST['password']))
        &&(!isset($_SERVER['PHP_AUTH_USER'])&&!isset($_SERVER['PHP_AUTH_PW']))
    )) {
    $_SESSION['latitude'] = $_POST['latitude'];
    $_SESSION['longitude'] = $_POST['longitude'];
    $_SESSION['finds'] = $_POST['finds'];
    //if user isn't authorized we send him authorization form
    if($versionInt && $versionInt < 90) {
        header('HTTP/1.1 401 Unauthorized');
    } else {
        header('WWW-Authenticate: Basic realm="Geocaching.com"');
        header('HTTP/1.0 401 Unauthorized');
    }
} else {
    if($versionInt && $versionInt < 90) {
        $login = $_POST['login'];
        $password = $_POST['password'];
    } else {
        $login = $_SERVER['PHP_AUTH_USER'];
        $password = $_SERVER['PHP_AUTH_PW'];
    }

    //login to geocaching.com (see functions.php)
    if(isset($login)&&isset($password))
    {
        login($login, $password);
    }

$url = "http://www.geocaching.com/seek/log.aspx?guid=".$_GET['guid'];
$req = & new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
$req->sendRequest();
$response = $req->getResponseBody();


}
}

//get cache coordinates

$url = "http://www.geocaching.com/seek/cache_details.aspx?guid=".$_GET['guid'];
$req = & new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
$req->sendRequest();
$response = $req->getResponseBody();

$position = 0;
$lat = parseBetween('href="/wpt/?lat=', '&amp;');
$lon = parseBetween('lon=', '&amp;');

//get actual time on coordinates

$url = "http://ws.geonames.org/timezone?lat=".$lat."&lng=".$lon;
$zonereq = & new HTTP_Request($url);
$zonereq->setMethod(HTTP_REQUEST_METHOD_GET);
$zonereq->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$zonereq->sendRequest();
$zone = $zonereq->getResponseBody();
$sxe = simplexml_load_string($zone);
$dom_sxe = dom_import_simplexml($sxe);
$doc = new DOMDocument('1.0');
$dom_sxe = $doc->importNode($dom_sxe, true);
$dom_sxe = $doc->appendChild($dom_sxe);

$time = date('G:i', strtotime($doc->getElementsByTagName("time")->item(0)->textContent));


$url = "http://www.geocaching.com/seek/log.aspx?guid=".$_GET['guid'];
$req = & new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
$req->sendRequest();
$response = $req->getResponseBody();

$position = strpos($response, '__EVENTTARGET');
$eventtarget = parseBetween('value="', '/>');
$eventtarget = trim(str_replace('"', '', $eventtarget));

$position = strpos($response, '__EVENTARGUMENT');
$eventargument = parseBetween('value="', '/>');
$eventargument = trim(str_replace('"', '', $eventargument));

$position = strpos($response, '__LASTFOCUS');
$lastfocus = parseBetween('value="', '/>');
$lastfocus = trim(str_replace('"', '', $lastfocus));

$position = strpos($response, '__VIEWSTATE');
$viewstate = parseBetween('value="', '/>');
$viewstate = trim(str_replace('"', '', $viewstate));



$url = "http://www.geocaching.com/seek/log.aspx?guid=".$_GET["guid"];
$req = & new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);

if ($_POST['ctl00$ContentBody$LogBookPanel1$ddLogType'] == 2) $_POST['ctl00_ContentBody_LogBookPanel1_tbLogInfo'] = $time.'
'.$_POST['ctl00_ContentBody_LogBookPanel1_tbLogInfo'].'

Cache found and logged using Locify http://locify.com';
foreach ($_POST as $key => $val) {
    $req->addPostData($key, $val);
}

$req->addPostData('__EVENTTARGET', $eventtarget);
$req->addPostData('__EVENTARGUMENT', $eventargument);
$req->addPostData('__LASTFOCUS', $lastfocus);
$req->addPostData('__VIEWSTATE', $viewstate);

$req->sendRequest();
$response = $req->getResponseBody();
if (!strpos($response, '<span id="ErrorText"')) {
	$html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>Info</title>
              </head>
              <body class="alert">
            <p class="text">'.$lang['log_save'].'.</p>
        <locify:timeout value="6000" />';
} else {
	$html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>Warning</title>
              </head>
              <body class="alert">
            <p class="text">'.$lang['log_error'].'.</p>
       <locify:timeout value="6000" /></body></html>';

}
echo $html;
?>
